package com.example.gui;


import com.example.dao.UserDAO;
import com.example.model.CardInfo;
import com.example.model.TradeInfo;
import com.example.model.UserInfo;

import javax.swing.*;
import java.awt.*;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.List;

public class UserMainFrame extends JFrame {

    CardInfo cardInfo;
    UserInfo  userInfo;         // 当前用户
    UserDAO userDAO = new UserDAO();

    private JLabel menuBalanceLabel;  // 余额标签的引用
    private JPanel mainPanel;         // 主面板的引用

    public UserMainFrame(CardInfo cardInfo1) {

        cardInfo = cardInfo1;
        userInfo = userDAO.getUserInfoByCarID(cardInfo.getCustomerId());

        setTitle("个人银行系统 - 欢迎您，" + userInfo.getCustomerName());
        setSize(1000, 700);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        // 使用卡片布局实现功能切换
        CardLayout cardLayout = new CardLayout();
        JPanel mainPanel = new JPanel(cardLayout);

        // 创建功能按钮面板
        JPanel menuPanel = new JPanel(new GridLayout(0, 1, 10, 10));
        menuPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        menuPanel.setPreferredSize(new Dimension(200, getHeight()));
        menuPanel.setBackground(new Color(230, 240, 255)); // 浅蓝色背景

        // 显示账户信息
        JPanel accountPanel = new JPanel(new BorderLayout());
        accountPanel.setBackground(new Color(70, 130, 180)); // 深蓝色
        accountPanel.setBorder(BorderFactory.createEmptyBorder(15, 10, 15, 10));

        JLabel cardLabel = new JLabel("银行卡号: " + cardInfo.getCardId());
        cardLabel.setForeground(Color.WHITE);
        cardLabel.setFont(new Font("微软雅黑", Font.BOLD, 14));
        accountPanel.add(cardLabel, BorderLayout.NORTH);

        // 余额查询（实际应用中应从数据库获取）
        BigDecimal balance = cardInfo.getBalance();
        menuBalanceLabel  = new JLabel("余额: " + formatCurrency(balance));
        menuBalanceLabel.setForeground(Color.WHITE);
        menuBalanceLabel.setFont(new Font("微软雅黑", Font.BOLD, 18));
        accountPanel.add(menuBalanceLabel, BorderLayout.CENTER);

        menuPanel.add(accountPanel);

        // 按钮样式设置
        Font buttonFont = new Font("微软雅黑", Font.PLAIN, 16);
        Color buttonColor = new Color(70, 130, 180);

        // 创建功能按钮
        JButton depositBtn = createMenuButton("存   款", buttonFont, buttonColor);
        depositBtn.addActionListener(e -> cardLayout.show(mainPanel, "deposit"));

        JButton withdrawBtn = createMenuButton("取   款", buttonFont, buttonColor);
        withdrawBtn.addActionListener(e -> cardLayout.show(mainPanel, "withdraw"));

        JButton transferBtn = createMenuButton("转   账", buttonFont, buttonColor);
        transferBtn.addActionListener(e -> cardLayout.show(mainPanel, "transfer"));

        JButton balanceBtn = createMenuButton("余额查询", buttonFont, buttonColor);
        balanceBtn.addActionListener(e -> cardLayout.show(mainPanel, "balance"));

        JButton reportBtn = createMenuButton("挂   失", buttonFont, new Color(200, 0, 0)); // 红色按钮
        reportBtn.addActionListener(e -> cardLayout.show(mainPanel, "report"));

        JButton changePwdBtn = createMenuButton("修改密码", buttonFont, buttonColor);
        changePwdBtn.addActionListener(e -> cardLayout.show(mainPanel, "password"));

        // 添加按钮到菜单面板
        menuPanel.add(depositBtn);
        menuPanel.add(withdrawBtn);
        menuPanel.add(transferBtn);
        menuPanel.add(balanceBtn);
        menuPanel.add(reportBtn);
        menuPanel.add(changePwdBtn);

        // 底部退出按钮
        JButton logoutBtn = createMenuButton("退出系统", buttonFont, new Color(139, 69, 19)); // 棕色按钮
        logoutBtn.addActionListener(e -> {
            int confirm = JOptionPane.showConfirmDialog(
                    this,
                    "确定要退出系统吗？",
                    "退出确认",
                    JOptionPane.YES_NO_OPTION
            );

            if (confirm == JOptionPane.YES_OPTION) {
                new StartFrame().setVisible(true);
                dispose();
            }
        });
        menuPanel.add(Box.createVerticalGlue()); // 填充空白区域
        menuPanel.add(logoutBtn);

        // 添加各功能界面到主面板
        mainPanel.add(new DepositPanel(), "deposit");
        mainPanel.add(new WithdrawPanel(), "withdraw");
        mainPanel.add(new TransferPanel(), "transfer");
        mainPanel.add(new BalancePanel(balance), "balance");
        mainPanel.add(new ReportLossPanel(), "report");
        mainPanel.add(new ChangePasswordPanel(), "password");

        // 主面板引用
        this.mainPanel =  mainPanel;

        // 默认显示存款界面
        cardLayout.show(mainPanel, "deposit");

        // 设置界面布局
        JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, menuPanel, mainPanel);
        splitPane.setDividerLocation(200);
        splitPane.setDividerSize(3);
        splitPane.setEnabled(false); // 禁止调整分隔线

        add(splitPane);

//        balanceLabel.setText("1234");

    }


    public void refreshBalance() {
        // 从数据库重新获取最新余额
        cardInfo = userDAO.getCardInfoByCardId(cardInfo.getCardId());
        menuBalanceLabel.setText("余额: " + formatCurrency(cardInfo.getBalance()));

        // 刷新余额面板
        Component[] components = mainPanel.getComponents();
        for (Component comp : components) {
            if (comp instanceof BalancePanel) {
                mainPanel.remove(comp);
                break;
            }
        }
        mainPanel.add(new BalancePanel(cardInfo.getBalance()), "balance");
    }

    // 创建统一风格的菜单按钮
    private JButton createMenuButton(String text, Font font, Color bgColor) {
        JButton button = new JButton(text);
        button.setFont(font);
        button.setFocusPainted(false);
        button.setBackground(bgColor);
        button.setForeground(Color.WHITE);
        button.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createRaisedBevelBorder(),
                BorderFactory.createEmptyBorder(10, 15, 10, 15)
        ));
        button.setAlignmentX(Component.CENTER_ALIGNMENT);
        return button;
    }

    // 格式化货币显示
    private static String formatCurrency(BigDecimal amount) {
        DecimalFormat formatter = new DecimalFormat("¥ #,##0.00");
        return formatter.format(amount);
    }

    // ============ 功能面板实现 ============

    // 存款面板
    class DepositPanel extends JPanel {
        public DepositPanel() {
            setLayout(new BorderLayout());
            setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

            // 标题
            JLabel titleLabel = new JLabel("存款操作", JLabel.CENTER);
            titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 24));
            titleLabel.setForeground(new Color(0, 100, 0)); // 绿色标题
            add(titleLabel, BorderLayout.NORTH);

            // 输入面板
            JPanel inputPanel = new JPanel(new GridBagLayout());
            inputPanel.setBorder(BorderFactory.createTitledBorder("请输入存款信息"));
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.insets = new Insets(10, 10, 10, 10);
            gbc.anchor = GridBagConstraints.WEST;

            // 当前卡号
            JLabel cardLabel = new JLabel("银行卡号:");
            cardLabel.setFont(new Font("宋体", Font.BOLD, 16));
            gbc.gridx = 0; gbc.gridy = 0;
            inputPanel.add(cardLabel, gbc);

            JTextField cardField = new JTextField(20);
            cardField.setEditable(false);
            cardField.setFont(new Font("宋体", Font.PLAIN, 16));
            cardField.setText(cardInfo.getCardId());
            gbc.gridx = 1; gbc.gridy = 0;
            inputPanel.add(cardField, gbc);

            // 存款金额
            JLabel amountLabel = new JLabel("存款金额:");
            amountLabel.setFont(new Font("宋体", Font.BOLD, 16));
            gbc.gridx = 0; gbc.gridy = 1;
            inputPanel.add(amountLabel, gbc);

            JTextField amountField = new JTextField(15);
            amountField.setFont(new Font("宋体", Font.PLAIN, 16));
            gbc.gridx = 1; gbc.gridy = 1;
            inputPanel.add(amountField, gbc);

            // +++ 新增：存款类型下拉框 +++
            JLabel typeLabel = new JLabel("存款类型:");
            typeLabel.setFont(new Font("宋体", Font.BOLD, 16));
            gbc.gridx = 0; gbc.gridy = 2;
            inputPanel.add(typeLabel, gbc);

            List<String> depositTypesList = userDAO.getAllDepositType();
            String[] depositTypes =  depositTypesList.toArray(new String[0]);
            JComboBox<String> typeComboBox = new JComboBox<>(depositTypes);
            typeComboBox.setFont(new Font("宋体", Font.PLAIN, 16));
            gbc.gridx = 1; gbc.gridy = 2;
            inputPanel.add(typeComboBox, gbc);

            // 备注
            JLabel remarkLabel = new JLabel("备    注:");
            remarkLabel.setFont(new Font("宋体", Font.BOLD, 16));
            gbc.gridx = 0; gbc.gridy = 3;
            inputPanel.add(remarkLabel, gbc);

            JTextField remarkField = new JTextField(20);
            remarkField.setFont(new Font("宋体", Font.PLAIN, 16));
            gbc.gridx = 1; gbc.gridy = 3;
            inputPanel.add(remarkField, gbc);

            // 操作按钮
            JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 10));

            JButton confirmBtn = new JButton("确认存款");
            confirmBtn.setFont(new Font("微软雅黑", Font.BOLD, 16));
            confirmBtn.setBackground(new Color(0, 150, 0)); // 绿色
            confirmBtn.setForeground(Color.WHITE);
            confirmBtn.addActionListener(e -> {
                String amountText = amountField.getText();                  // 存款金额
                String depositType = (String) typeComboBox.getSelectedItem();
                String remarkText = remarkField.getText();
                try {
                    BigDecimal amount = new BigDecimal(amountText);
                    if (amount.compareTo(BigDecimal.ZERO) <= 0) {
                        JOptionPane.showMessageDialog(this, "存款金额必须大于0", "输入错误", JOptionPane.ERROR_MESSAGE);
                    } else {
                        // 执行存款操作
                        userDAO.Saving(cardInfo.getCardId(), amount, depositType);


                        // 调用刷新方法
                        UserMainFrame.this.refreshBalance();

                        // 添加存款记录
                        TradeInfo tradeInfo = new TradeInfo();
                        tradeInfo.setCardId(cardInfo.getCardId());
                        tradeInfo.setTradeMoney(amount);
                        tradeInfo.setTradeType("存款");
                        tradeInfo.setTradeId(String.valueOf(System.currentTimeMillis()));
                        tradeInfo.setRemark(remarkText);

                        // 插入存款记录
                        userDAO.insertTradeinfo(tradeInfo);

                        // 清空输入并提示
                        amountField.setText("");
                        remarkField.setText("");
                        JOptionPane.showMessageDialog(
                                this,
                                "存款成功！当前余额: " + formatCurrency(cardInfo.getBalance()),
                                "存款成功",
                                JOptionPane.INFORMATION_MESSAGE
                        );
                    }
                } catch (NumberFormatException ex) {
                    JOptionPane.showMessageDialog(this, "请输入有效的金额", "输入错误", JOptionPane.ERROR_MESSAGE);
                }
            });
            buttonPanel.add(confirmBtn);

            JButton resetBtn = new JButton("重置");
            resetBtn.setFont(new Font("微软雅黑", Font.BOLD, 16));
            resetBtn.setBackground(new Color(150, 150, 150)); // 灰色
            resetBtn.setForeground(Color.WHITE);
            resetBtn.addActionListener(e -> {
                amountField.setText("");
                remarkField.setText("");
            });
            buttonPanel.add(resetBtn);

            // 添加到主面板
            add(inputPanel, BorderLayout.CENTER);
            add(buttonPanel, BorderLayout.SOUTH);
        }
    }

    // 取款面板
    class WithdrawPanel extends JPanel {
        public WithdrawPanel() {
            setLayout(new BorderLayout());
            setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

            // 标题
            JLabel titleLabel = new JLabel("取款操作", JLabel.CENTER);
            titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 24));
            titleLabel.setForeground(new Color(150, 0, 0)); // 红色标题
            add(titleLabel, BorderLayout.NORTH);

            // 输入面板
            JPanel inputPanel = new JPanel(new GridLayout(3, 2, 15, 15));
            inputPanel.setBorder(BorderFactory.createTitledBorder("请输入取款信息"));

            // 当前卡号
            inputPanel.add(new JLabel("银行卡号:"));
            JTextField cardField = new JTextField(cardInfo.getCardId());
            cardField.setEditable(false);
            cardField.setFont(new Font("宋体", Font.PLAIN, 16));
            inputPanel.add(cardField);

            // 取款金额
            inputPanel.add(new JLabel("取款金额:"));
            JTextField amountField = new JTextField();
            amountField.setFont(new Font("宋体", Font.PLAIN, 16));
            inputPanel.add(amountField);

            // 输入备注
            inputPanel.add(new JLabel("备注:"));
            JTextField remarkField = new JTextField();
            remarkField.setFont(new Font("宋体", Font.PLAIN, 16));
            inputPanel.add(remarkField);

            // 操作按钮
            JPanel buttonPanel = new JPanel();
            buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));

            JButton confirmBtn = new JButton("确认取款");
            confirmBtn.setFont(new Font("微软雅黑", Font.BOLD, 16));
            confirmBtn.setBackground(new Color(150, 0, 0)); // 红色
            confirmBtn.setForeground(Color.WHITE);
            confirmBtn.addActionListener(e -> {
                String amountText = amountField.getText();
                String remark = remarkField.getText();
                try {
                    BigDecimal amount = new BigDecimal(amountText);

                    if (amount.compareTo(BigDecimal.ZERO) <= 0) {
                        JOptionPane.showMessageDialog(this, "取款金额必须大于0", "输入错误", JOptionPane.ERROR_MESSAGE);
                    } else {
                        // 实际应用中应执行取款操作
                        if(userDAO.withdrawal(cardInfo.getCardId(), amount)) {
                            JOptionPane.showMessageDialog(this,
                                    "成功取出 " + formatCurrency(amount),
                                    "取款成功",
                                    JOptionPane.INFORMATION_MESSAGE);
                            amountField.setText("");
                            remarkField.setText("");
                            // 调用刷新方法
                            UserMainFrame.this.refreshBalance();

                            // 添加取款记录
                            TradeInfo tradeInfo = new TradeInfo();
                            tradeInfo.setCardId(cardInfo.getCardId());
                            tradeInfo.setTradeMoney(amount);
                            tradeInfo.setTradeType("取款");
                            tradeInfo.setTradeId(String.valueOf(System.currentTimeMillis()));
                            tradeInfo.setRemark(remark);

                            // 插入存款记录
                            userDAO.insertTradeinfo(tradeInfo);

                        }else {
                            JOptionPane.showMessageDialog(this,
                                    "请检查取款金额，不得大于余额！ ",
                                    "取款失败",
                                    JOptionPane.INFORMATION_MESSAGE);
                            amountField.setText("");
                            remarkField.setText("");
                        }


                    }
                } catch (NumberFormatException ex) {
                    JOptionPane.showMessageDialog(this, "请输入有效的金额", "输入错误", JOptionPane.ERROR_MESSAGE);
                }
            });
            buttonPanel.add(confirmBtn);

            JButton resetBtn = new JButton("重置");
            resetBtn.setFont(new Font("微软雅黑", Font.BOLD, 16));
            resetBtn.setBackground(new Color(150, 150, 150)); // 灰色
            resetBtn.setForeground(Color.WHITE);
            resetBtn.addActionListener(e -> {
                amountField.setText("");
                remarkField.setText("");
            });
            buttonPanel.add(resetBtn);

            // 添加快速取款按钮
            String[] quickAmounts = {"100", "500", "1000", "2000", "5000"};
            for (String amt : quickAmounts) {
                JButton quickBtn = new JButton(amt);
                quickBtn.setFont(new Font("微软雅黑", Font.PLAIN, 14));
                quickBtn.addActionListener(e -> amountField.setText(amt));
                buttonPanel.add(Box.createHorizontalStrut(5));
                buttonPanel.add(quickBtn);
            }

            // 添加到主面板
            add(inputPanel, BorderLayout.CENTER);
            add(buttonPanel, BorderLayout.SOUTH);
        }
    }

    // 转账面板
    class TransferPanel extends JPanel {
        public TransferPanel() {
            setLayout(new BorderLayout());
            setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

            // 标题
            JLabel titleLabel = new JLabel("转账操作", JLabel.CENTER);
            titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 24));
            titleLabel.setForeground(new Color(70, 70, 180)); // 蓝色标题
            add(titleLabel, BorderLayout.NORTH);

            // 输入面板
            JPanel inputPanel = new JPanel(new GridLayout(5, 2, 10, 15));
            inputPanel.setBorder(BorderFactory.createTitledBorder("请输入转账信息"));

            // 转出账号
            inputPanel.add(new JLabel("转出账户:"));
            JTextField fromField = new JTextField(cardInfo.getCardId());
            fromField.setEditable(false);
            fromField.setFont(new Font("宋体", Font.PLAIN, 16));
            inputPanel.add(fromField);

            // 转入账号
            inputPanel.add(new JLabel("转入账户:"));
            JTextField toField = new JTextField();
            toField.setFont(new Font("宋体", Font.PLAIN, 16));
            inputPanel.add(toField);

            // 转账金额
            inputPanel.add(new JLabel("转账金额:"));
            JTextField amountField = new JTextField();
            amountField.setFont(new Font("宋体", Font.PLAIN, 16));
            inputPanel.add(amountField);

            // 收款人姓名
            inputPanel.add(new JLabel("收款人姓名:"));
            JTextField nameField = new JTextField();
            nameField.setFont(new Font("宋体", Font.PLAIN, 16));
            inputPanel.add(nameField);

            // 备注
            inputPanel.add(new JLabel("备    注:"));
            JTextField remarkField = new JTextField();
            remarkField.setFont(new Font("宋体", Font.PLAIN, 16));
            inputPanel.add(remarkField);

            // 操作按钮
            JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 10));

            JButton confirmBtn = new JButton("确认转账");
            confirmBtn.setFont(new Font("微软雅黑", Font.BOLD, 16));
            confirmBtn.setBackground(new Color(70, 130, 180));
            confirmBtn.setForeground(Color.WHITE);
            confirmBtn.addActionListener(e -> {
                String toAccount = toField.getText();
                String amountText = amountField.getText();
                String transferToName = nameField.getText();
                String remark = remarkField.getText();

                if (toAccount.length() != 16 || !toAccount.startsWith("10103576")) {
                    JOptionPane.showMessageDialog(this, "转入账户格式不正确", "输入错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }

                try {
                    BigDecimal amount = new BigDecimal(amountText);
                    if (amount.compareTo(BigDecimal.ZERO) <= 0 || amount.compareTo(cardInfo.getBalance())<-1) {
                        JOptionPane.showMessageDialog(this, "转账金额必须大于0且小于余额", "输入错误", JOptionPane.ERROR_MESSAGE);
                    } else {
                        // 实际应用中应执行转账操作
                        if(userDAO.transferMoney(cardInfo.getCardId(), toAccount, amount, transferToName)){
                            JOptionPane.showMessageDialog(this,
                                    "成功转账 " + formatCurrency(amount) + " 至账户 " + toAccount,
                                    "转账成功",
                                    JOptionPane.INFORMATION_MESSAGE);
                            toField.setText("");
                            amountField.setText("");
                            nameField.setText("");
                            remarkField.setText("");

                            // 调用刷新方法
                            UserMainFrame.this.refreshBalance();

                            // 添加转账记录
                            TradeInfo tradeInfo = new TradeInfo();
                            tradeInfo.setCardId(cardInfo.getCardId());
                            tradeInfo.setTradeMoney(amount);
                            tradeInfo.setTradeType("转账");
                            tradeInfo.setTradeId(String.valueOf(System.currentTimeMillis()));
                            tradeInfo.setRemark(remark);

                            userDAO.insertTradeinfo(tradeInfo);

                        }else{
                            JOptionPane.showMessageDialog(this,
                                    "输入信息有误，请检查后重试！",
                                    "转账失败",
                                    JOptionPane.INFORMATION_MESSAGE);
                            toField.setText("");
                            amountField.setText("");
                            nameField.setText("");
                            remarkField.setText("");
                        }

                    }
                } catch (NumberFormatException ex) {
                    JOptionPane.showMessageDialog(this, "请输入有效的金额", "输入错误", JOptionPane.ERROR_MESSAGE);
                }
            });
            buttonPanel.add(confirmBtn);

            JButton resetBtn = new JButton("重置");
            resetBtn.setFont(new Font("微软雅黑", Font.BOLD, 16));
            resetBtn.setBackground(new Color(150, 150, 150)); // 灰色
            resetBtn.setForeground(Color.WHITE);
            resetBtn.addActionListener(e -> {
                toField.setText("");
                amountField.setText("");
                nameField.setText("");
                remarkField.setText("");
            });
            buttonPanel.add(resetBtn);

            // 添加到主面板
            add(inputPanel, BorderLayout.CENTER);
            add(buttonPanel, BorderLayout.SOUTH);
        }
    }

    // 余额查询面板
    static class BalancePanel extends JPanel {
        public BalancePanel(BigDecimal balance) {
            setLayout(new GridBagLayout());
            setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

            // 标题
            JLabel titleLabel = new JLabel("账户余额查询", JLabel.CENTER);
            titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 24));
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0; gbc.gridy = 0;
            gbc.insets = new Insets(0, 0, 30, 0);
            add(titleLabel, gbc);

            // 余额显示面板
            JPanel balancePanel = new JPanel(new BorderLayout());
            balancePanel.setBorder(BorderFactory.createCompoundBorder(
                    BorderFactory.createLineBorder(new Color(70, 130, 180), 2),
                    BorderFactory.createEmptyBorder(40, 40, 40, 40)
            ));
            balancePanel.setBackground(new Color(230, 245, 255)); // 浅蓝色背景

            JLabel balanceLabel = new JLabel(formatCurrency(balance), JLabel.CENTER);
            balanceLabel.setFont(new Font("微软雅黑", Font.BOLD, 42));
            balanceLabel.setForeground(new Color(0, 100, 0)); // 绿色数字
            balancePanel.add(balanceLabel, BorderLayout.CENTER);

            JLabel accountLabel = new JLabel("账户余额", JLabel.CENTER);
            accountLabel.setFont(new Font("微软雅黑", Font.PLAIN, 20));
            balancePanel.add(accountLabel, BorderLayout.NORTH);

            gbc.gridx = 0; gbc.gridy = 1;
            gbc.insets = new Insets(0, 0, 0, 0);
            add(balancePanel, gbc);
        }
    }

    // 挂失面板
    class ReportLossPanel extends JPanel {
        public ReportLossPanel() {
            setLayout(new BorderLayout());
            setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

            // 标题
            JLabel titleLabel = new JLabel("银行卡挂失", JLabel.CENTER);
            titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 24));
            titleLabel.setForeground(Color.RED); // 红色标题
            add(titleLabel, BorderLayout.NORTH);

            // 警告信息
            JPanel warningPanel = new JPanel(new BorderLayout());
            warningPanel.setBorder(BorderFactory.createCompoundBorder(
                    BorderFactory.createLineBorder(Color.RED, 2),
                    BorderFactory.createEmptyBorder(20, 20, 20, 20)
            ));
            warningPanel.setBackground(new Color(255, 240, 240)); // 浅红色背景

            JLabel warningLabel = new JLabel("<html><center>警告：挂失操作将立即冻结您的银行卡，在解挂之前无法进行任何交易操作！<br/>请谨慎操作。</center></html>");
            warningLabel.setFont(new Font("微软雅黑", Font.BOLD, 16));
            warningLabel.setForeground(Color.RED);
            warningLabel.setHorizontalAlignment(SwingConstants.CENTER);
            warningPanel.add(warningLabel);

            // 验证面板
            JPanel verifyPanel = new JPanel(new GridLayout(2, 2, 10, 15));
            verifyPanel.setBorder(BorderFactory.createTitledBorder("身份验证"));

            verifyPanel.add(new JLabel("身份证号码:"));
            JTextField pidField = new JTextField();
            pidField.setFont(new Font("宋体", Font.PLAIN, 16));
            verifyPanel.add(pidField);

            verifyPanel.add(new JLabel("手机号码:"));
            JTextField phoneField = new JTextField();
            phoneField.setFont(new Font("宋体", Font.PLAIN, 16));
            verifyPanel.add(phoneField);

            // 操作按钮
            JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 10));

            JButton reportBtn = new JButton("确认挂失");
            reportBtn.setFont(new Font("微软雅黑", Font.BOLD, 16));
            reportBtn.setBackground(Color.RED);
            reportBtn.setForeground(Color.WHITE);
            reportBtn.addActionListener(e -> {
                int confirm = JOptionPane.showConfirmDialog(
                        this,
                        "确定要挂失银行卡吗？",
                        "挂失确认",
                        JOptionPane.YES_NO_OPTION,
                        JOptionPane.WARNING_MESSAGE
                );

                if (confirm == JOptionPane.YES_OPTION) {
                    // 实际应用中应执行挂失操作
                    String userPid =  pidField.getText();
                    String userPhone =  phoneField.getText();

                    if(userPhone.equals(userInfo.getTelephone())){
                        if(userPid.equals(userInfo.getPid())){
                            if(userDAO.reportLoss(cardInfo.getCardId())){
                                JOptionPane.showMessageDialog(this,
                                        "银行卡挂失成功！请尽快到银行网点办理相关手续。",
                                        "挂失成功",
                                        JOptionPane.INFORMATION_MESSAGE);
                                pidField.setText("");
                                phoneField.setText("");


                            }else {
                                JOptionPane.showMessageDialog(this,
                                        "系统错误，请重试或线下办理！",
                                        "错误",
                                        JOptionPane.INFORMATION_MESSAGE);
                                pidField.setText("");
                                phoneField.setText("");
                            }

                        }else{
                            JOptionPane.showMessageDialog(this,
                                    "身份证号码错误，请重新输入！",
                                    "挂失失败",
                                    JOptionPane.INFORMATION_MESSAGE);
                            pidField.setText("");
                            phoneField.setText("");
                        }
                    }else{
                        JOptionPane.showMessageDialog(this,
                                "手机号码错误，请重新输入！",
                                "挂失失败",
                                JOptionPane.INFORMATION_MESSAGE);
                        pidField.setText("");
                        phoneField.setText("");
                    }

                }
            });
            buttonPanel.add(reportBtn);

            JButton cancelBtn = new JButton("取消挂失");
            cancelBtn.setFont(new Font("微软雅黑", Font.BOLD, 16));
            cancelBtn.setBackground(new Color(150, 150, 150)); // 灰色
            cancelBtn.setForeground(Color.WHITE);
            buttonPanel.add(cancelBtn);

            // 添加到主面板
            add(warningPanel, BorderLayout.NORTH);
            add(verifyPanel, BorderLayout.CENTER);
            add(buttonPanel, BorderLayout.SOUTH);
        }
    }

    // 修改密码面板
    class ChangePasswordPanel extends JPanel {
        public ChangePasswordPanel() {
            setLayout(new BorderLayout());
            setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

            // 标题
            JLabel titleLabel = new JLabel("修改账户密码", JLabel.CENTER);
            titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 24));
            titleLabel.setForeground(new Color(0, 0, 150)); // 深蓝色标题
            add(titleLabel, BorderLayout.NORTH);

            // 输入面板
            JPanel inputPanel = new JPanel(new GridLayout(4, 2, 10, 15));
            inputPanel.setBorder(BorderFactory.createTitledBorder("密码修改"));

            inputPanel.add(new JLabel("原密码:"));
            JPasswordField oldPwdField = new JPasswordField();
            oldPwdField.setFont(new Font("宋体", Font.PLAIN, 16));
            inputPanel.add(oldPwdField);

            inputPanel.add(new JLabel("新密码:"));
            JPasswordField newPwdField = new JPasswordField();
            newPwdField.setFont(new Font("宋体", Font.PLAIN, 16));
            inputPanel.add(newPwdField);

            inputPanel.add(new JLabel("确认新密码:"));
            JPasswordField confirmPwdField = new JPasswordField();
            confirmPwdField.setFont(new Font("宋体", Font.PLAIN, 16));
            inputPanel.add(confirmPwdField);

            // 密码强度提示
            JLabel strengthLabel = new JLabel("密码需包含6位以上字母和数字");
            strengthLabel.setForeground(Color.GRAY);
            strengthLabel.setFont(new Font("微软雅黑", Font.PLAIN, 12));
            inputPanel.add(new JLabel());
            inputPanel.add(strengthLabel);

            // 操作按钮
            JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 10));

            JButton changeBtn = new JButton("确认修改");
            changeBtn.setFont(new Font("微软雅黑", Font.BOLD, 16));
            changeBtn.setBackground(new Color(70, 130, 180));
            changeBtn.setForeground(Color.WHITE);
            changeBtn.addActionListener(e -> {
                String oldPwd = oldPwdField.getText();
                String newPwd = new String(newPwdField.getPassword());
                String confirmPwd = new String(confirmPwdField.getPassword());

                if (!newPwd.equals(confirmPwd)) {
                    JOptionPane.showMessageDialog(this, "两次输入的密码不一致", "密码错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }

                if (newPwd.length() < 6) {
                    JOptionPane.showMessageDialog(this, "密码长度至少6位", "密码不符合要求", JOptionPane.ERROR_MESSAGE);
                    return;
                }

                // 实际应用中应执行修改密码操作
                if(oldPwd.equals(cardInfo.getPass())){
                    if(userDAO.changePwd(cardInfo.getCardId(), newPwd)){
                        JOptionPane.showMessageDialog(this,
                                "密码修改成功！请妥善保管您的新密码。",
                                "修改成功",
                                JOptionPane.INFORMATION_MESSAGE);
                        oldPwdField.setText("");
                        newPwdField.setText("");
                        confirmPwdField.setText("");
                    }else {
                        JOptionPane.showMessageDialog(this,
                                "操作失败，请重试或线下操作！",
                                "修改失败",
                                JOptionPane.INFORMATION_MESSAGE);
                        oldPwdField.setText("");
                        newPwdField.setText("");
                        confirmPwdField.setText("");
                    }
                }else{
                    JOptionPane.showMessageDialog(this, "原密码输入错误！", "密码错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }

            });
            buttonPanel.add(changeBtn);

            JButton resetBtn = new JButton("重置");
            resetBtn.setFont(new Font("微软雅黑", Font.BOLD, 16));
            resetBtn.setBackground(new Color(150, 150, 150)); // 灰色
            resetBtn.setForeground(Color.WHITE);
            resetBtn.addActionListener(e -> {
                oldPwdField.setText("");
                newPwdField.setText("");
                confirmPwdField.setText("");
            });
            buttonPanel.add(resetBtn);

            // 添加到主面板
            add(inputPanel, BorderLayout.CENTER);
            add(buttonPanel, BorderLayout.SOUTH);
        }
    }


}